$(document).ready(function(){
    $('#localApproved').change();
    $('#status').val($('#siteStatusHidden').val());
    validateOngoing();
    $('#formEdit').valid();
});

function getSiteName(investigatorId, url){
    if(investigatorId == -1){
        $("#siteName").val('');
        return;
    }
    $.ajax({
        traditional: true,
        type: "POST",
        url: url,
        data: { "investigatorId": investigatorId},
        dataType:'html',
        success: function(result)
        {
            var siteName = result.split('-')[0];
            var siteStatus = result.split('-')[1];
            $("#siteName").val(siteName);
            $("#siteStatus").val(siteStatus);
        }
    });
}

function ifOptionHide(value, option, idToHide){
    if(value == option){
        $('#'+idToHide).parent().parent().hide();
    }else
        $('#'+idToHide).parent().parent().show();
}

function localApprovedCheck(value){
    if(value == 'CCIS'){
        $('#localApprovalFile').parent().parent().show();
        $('#otherLocalApproved').parent().parent().hide();
    }else{
        $('#localApprovalFile').parent().parent().hide();
        if(value == 'Other'){
            $('#otherLocalApproved').parent().parent().show();
        }
        else
            $('#otherLocalApproved').parent().parent().hide();
    }
}

function specialRecruitmentActionsCheck(value){
    ifOptionHide(value, 'NO', 'recruitmentActionsDescription');
}

function inspectionCheck(value){
    ifOptionHide(value, 'NO', 'inspectionMOH');
    ifOptionHide(value, 'NO', 'inspectionAction');
}

function bmsAuditCheck(value){
    ifOptionHide(value, 'NO', 'outcomeBms');
}

function ecAuditCheck(value){
    ifOptionHide(value, 'NO', 'outcomeEc');
}

function checkInvestigator(value, url){
    if(value == -1){
        clearForm();
        return;
    }
    $('#listOngoing > tr').each(function(index, row){
        if(row.children[6].children[0].value == value){
            $('#formEdit').block({
                message: '<h1>Loading...</h1>',
                css: { border: '3px solid #000' }
            });
            row.children[4].children[0].submit();
            return;
        }
    });
    clearForm();
    $('#principalInvestigator').val(value);
    getSiteName(value, url);
}

function checkSiteStatus(select){
    if(select.value == 'Enrollment Closed' && $('#actualLpfv').val() == ''){
        $(select).val($('#siteStatusHidden').val());
        alert("'Enrollment Closed' status cannot be set here. It is set by inserting the Actual LPFV date");
    }
}

function validateOngoing(){
    jQuery.validator.addMethod("valueNotEquals", function(value, element, arg){
        return value != arg;
    });

    jQuery.validator.addMethod('required_group', function(val, el) {
        var $module = $(el).parents('table');
        return $module.find('.required_group:filled').length;
    });

    jQuery.validator.addClassRules('required_group', {
        'required_group' : true
    });

    jQuery.validator.messages.required_group = '*';

    $("#formEdit")
        .validate(
        {
            rules : {
                'investigatorId' : {
                    required:true,
                    valueNotEquals:"-1"
                }
            },
            messages : {
                'investigatorId': {
                    valueNotEquals: "*"
                }
            }
        })
}